package com.ffzl.cloud.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.ffzl.cloud.R;
import com.ffzl.cloud.app.AppActivity;
import com.ffzl.cloud.app.AppFragment;
import com.ffzl.cloud.http.response.GrideviewItem;
import com.ffzl.cloud.http.response.UserInfoBean;
import com.ffzl.cloud.interfacenum.ValueEnum;
import com.ffzl.cloud.manager.ActivityManager;
import com.ffzl.cloud.other.DoubleClickHelper;
import com.ffzl.cloud.other.IntentKey;
import com.ffzl.cloud.ui.adapter.GrideViewAdapter;
import com.ffzl.cloud.ui.fragment.FindFragment;
import com.ffzl.cloud.ui.fragment.HomeFragment;
import com.ffzl.cloud.ui.fragment.MeFragment;
import com.ffzl.cloud.ui.fragment.MessageFragment;
import com.ffzl.cloud.utils.SPUserInfoUtil;
import com.ffzl.cloud.utils.SPUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.base.BaseAdapter;
import com.hjq.base.FragmentPagerAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 首页界面
 */
public final class HomeActivity extends AppActivity
        implements BottomNavigationView.OnNavigationItemSelectedListener,
        BaseAdapter.OnItemClickListener,
        BaseAdapter.OnItemLongClickListener,
        BaseAdapter.OnChildClickListener {

    private ViewPager mViewPager;
    private BottomNavigationView mBottomNavigationView;
    private RecyclerView main_gridView;//九宫格分配界面
    private List<GrideviewItem> grideviewItems = new ArrayList<>();
    private GrideViewAdapter grideViewAdapter;

    private FragmentPagerAdapter<AppFragment<?>> mPagerAdapter;

    public static void start(Context context) {
        start(context, HomeFragment.class);
    }

    public static void start(Context context, Class<? extends AppFragment<?>> fragmentClass) {
        Intent intent = new Intent(context, HomeActivity.class);
        intent.putExtra(IntentKey.INDEX, fragmentClass);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.home_activity;
    }

    @Override
    protected void initView() {
        mViewPager = findViewById(R.id.vp_home_pager);
        mBottomNavigationView = findViewById(R.id.bv_home_navigation);

        // 不使用图标默认变色
        mBottomNavigationView.setItemIconTintList(null);
        // 设置导航栏条目点击事件
        mBottomNavigationView.setOnNavigationItemSelectedListener(this);

        // 屏蔽底部导航栏长按文本提示
        Menu menu = mBottomNavigationView.getMenu();
        for (int i = 0; i < menu.size(); i++) {
            mBottomNavigationView.findViewById(menu.getItem(i).getItemId()).setOnLongClickListener(v -> true);
        }

        main_gridView = findViewById(R.id.main_gridView);


        getStatusBarConfig().titleBar(R.id.statusBar).init();
    }

    @Override
    protected void initData() {
        mPagerAdapter = new FragmentPagerAdapter<>(this);
        SPUserInfoUtil.getInstance().setUserInfo(new UserInfoBean("用户数据", "名称"));
//        mPagerAdapter.addFragment(HomeFragment.newInstance());
//        mPagerAdapter.addFragment(FindFragment.newInstance());
//        mPagerAdapter.addFragment(MessageFragment.newInstance());
//        mPagerAdapter.addFragment(MeFragment.newInstance());
//        mViewPager.setAdapter(mPagerAdapter);

        onNewIntent(getIntent());
        GrideviewItem grideviewItem = new GrideviewItem("学生", ValueEnum.VALUE_ENUM_STUDENT, "http://192.168.0.252:8080/", getResources().getDrawable(R.drawable.home_home_on_ic));
        grideviewItems.add(grideviewItem);
        grideviewItem = new GrideviewItem("老师", ValueEnum.VALUE_ENUM_TEACHER, "http://192.168.0.65:8081/", getResources().getDrawable(R.drawable.home_found_on_ic));
        grideviewItems.add(grideviewItem);
        grideviewItem = new GrideviewItem("管理", ValueEnum.VALUE_ENUM_ADMIN, "http://192.168.0.65:8080/", getResources().getDrawable(R.drawable.home_me_on_ic));
        grideviewItems.add(grideviewItem);
        for (int i = 0; i < 5; i++) {
            grideviewItem = new GrideviewItem("测试" + i, ValueEnum.VALUE_ENUM_VALUE + "ASDAD", "http://192.168.0.65:8080/", getResources().getDrawable(R.drawable.home_message_on_ic));
            grideviewItems.add(grideviewItem);
        }


        //在此处修改布局排列方向
        main_gridView.setLayoutManager(new GridLayoutManager(this, 3));
        grideViewAdapter = new GrideViewAdapter(this, grideviewItems);
        grideViewAdapter.setOnChildClickListener(R.id.grideview_main_linear, this);
        grideViewAdapter.setOnItemClickListener(this);
        grideViewAdapter.setOnItemLongClickListener(this);

        main_gridView.setAdapter(grideViewAdapter);


    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int fragmentIndex = mPagerAdapter.getFragmentIndex(getSerializable(IntentKey.INDEX));
        if (fragmentIndex == -1) {
            return;
        }

        mViewPager.setCurrentItem(fragmentIndex);
//        switch (fragmentIndex) {
//            case 0:
//                mBottomNavigationView.setSelectedItemId(R.id.menu_home);
//                BrowserActivity.start(getActivity(), "http://192.168.0.252:8081/");
//                break;
//            case 1:
//                mBottomNavigationView.setSelectedItemId(R.id.home_found);
//                BrowserActivity.start(getActivity(), "http://192.168.0.65:8081/");
//                break;
//            case 2:
//                mBottomNavigationView.setSelectedItemId(R.id.home_message);
//                BrowserActivity.start(getActivity(), "http://192.168.0.65:8080/");
//                break;
////            case 3:
////                mBottomNavigationView.setSelectedItemId(R.id.home_me);
////                break;
//            default:
//                break;
//        }
    }

    /**
     * {@link BottomNavigationView.OnNavigationItemSelectedListener}
     */

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == R.id.menu_home) {// 学生
            mViewPager.setCurrentItem(0);
            BrowserActivity.start(getActivity(), "http://192.168.0.252:8080/");
            return true;
        } else if (itemId == R.id.home_found) {// 老师
            mViewPager.setCurrentItem(1);
            BrowserActivity.start(getActivity(), "http://192.168.0.65:8081/");
            return true;
        } else if (itemId == R.id.home_message) {// 管理
            mViewPager.setCurrentItem(2);
            BrowserActivity.start(getActivity(), "http://192.168.0.65:8080/");
            return true;
        }
//        else if (itemId == R.id.home_me) {
//            mViewPager.setCurrentItem(3);
//            return true;
//        }
        return false;
    }

    @Override
    public void onBackPressed() {
        if (!DoubleClickHelper.isOnDoubleClick()) {
            toast(R.string.home_exit_hint);
            return;
        }

        // 移动到上一个任务栈，避免侧滑引起的不良反应
        moveTaskToBack(false);
        postDelayed(() -> {
            // 进行内存优化，销毁掉所有的界面
            ActivityManager.getInstance().finishAllActivities();
            // 销毁进程（注意：调用此 API 可能导致当前 Activity onDestroy 方法无法正常回调）
            // System.exit(0);
        }, 300);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mViewPager.setAdapter(null);
        mBottomNavigationView.setOnNavigationItemSelectedListener(null);
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        if (grideviewItems.get(position).getNameValue().contains(ValueEnum.VALUE_ENUM_STUDENT)) {

        } else if (grideviewItems.get(position).getNameValue().contains(ValueEnum.VALUE_ENUM_TEACHER)) {

        } else if (grideviewItems.get(position).getNameValue().contains(ValueEnum.VALUE_ENUM_ADMIN)) {

        }
        BrowserActivity.start(getActivity(), grideviewItems.get(position).getValueMess());
    }

    @Override
    public boolean onItemLongClick(RecyclerView recyclerView, View itemView, int position) {
        BrowserActivity.start(getActivity(), grideviewItems.get(position).getValueMess());
        return false;
    }

    @Override
    public void onChildClick(RecyclerView recyclerView, View childView, int position) {
        BrowserActivity.start(getActivity(), grideviewItems.get(position).getValueMess());
    }

}